package itesm.mx;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public abstract class QuizActivity extends Activity implements OnClickListener {
	
	private static final int DIALOG_GAME_OVER = 0;
	
	protected String[] countries;
	protected ArrayList<Integer> usedQuestions;
	
	protected int[] currentQuestion;
	protected int currentAnswer;
	
	protected int current;
	protected int correct;
	protected int total = 10;
	
	protected Toast feedback;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		current = 0;
		correct = 0;
		
		usedQuestions = new ArrayList<Integer>();
	}
	
	protected void nextQuestion() {
		Random rand = new Random();
		
		currentQuestion = new int[4];
		
		currentQuestion[0] = rand.nextInt(countries.length);
		do {
			currentQuestion[1] = rand.nextInt(countries.length);
		} while (currentQuestion[1] == currentQuestion[0]);
		do {
			currentQuestion[2] = rand.nextInt(countries.length);
		} while (currentQuestion[2] == currentQuestion[0] || currentQuestion[2] == currentQuestion[1]);
		do {
			currentQuestion[3] = rand.nextInt(countries.length);
		} while (currentQuestion[3] == currentQuestion[0] || currentQuestion[3] == currentQuestion[1] || currentQuestion[3] == currentQuestion[2]);
		
		currentAnswer = currentQuestion[rand.nextInt(4)];
	}
	
	protected abstract void displayQuestion();
	
	protected abstract int getId();
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		boolean currentCorrect = false;
		int answerid = -1;
		
		switch (id) {
		case R.id.answer1:
			answerid = 0;
			break;
		case R.id.answer2:
			answerid = 1;
			break;
		case R.id.answer3:
			answerid = 2;
			break;
		case R.id.answer4:
			answerid = 3;
			break;
		}
		
		if (currentQuestion[answerid] == currentAnswer) {
			currentCorrect = true;
			correct += 1;
		}
		
		String message = currentCorrect ? 
			getResources().getString(R.string.answer_correct) : 
			getResources().getString(R.string.answer_incorrect);
		
		if (feedback == null) {
			feedback = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		} else {
			feedback.setText(message);
		}
		
		feedback.show();
		
		usedQuestions.add(currentAnswer);
		current += 1;
		
		((TextView)findViewById(R.id.tvScore)).setText(""+correct);
		
		if (current >= total) { // this was last question
			// handle quiz end
			onFinish(correct);
			showDialog(DIALOG_GAME_OVER);
			//this.finish();
		} else { // generate next question
			do {
				nextQuestion();
			} while (usedQuestions.contains(currentAnswer));
			displayQuestion();
		}
	}
	
	protected abstract void onFinish(int score);
	
	protected void updateStats(int gameId, int score) {
		SharedPreferences prefs = getSharedPreferences(AndroidProject.DROID_TRIVIA_PREFERENCES, Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();

		switch (gameId) {
		case AndroidProject.QUIZ_FLAGS:
			editor.putInt(HighScores.FLAGS_TIMES, prefs.getInt(HighScores.FLAGS_TIMES, 0) + 1);
			if (score == 10) {
				editor.putInt(HighScores.FLAGS_WINS, prefs.getInt(HighScores.FLAGS_WINS, 0) + 1);
			}
			editor.putInt(HighScores.FLAGS_SCORE, prefs.getInt(HighScores.FLAGS_SCORE, 0) + score);
			break;
		case AndroidProject.QUIZ_MAPS:
			editor.putInt(HighScores.MAPS_TIMES, prefs.getInt(HighScores.MAPS_TIMES, 0) + 1);
			if (score == 10) {
				editor.putInt(HighScores.MAPS_WINS, prefs.getInt(HighScores.MAPS_WINS, 0) + 1);
			}
			editor.putInt(HighScores.MAPS_SCORE, prefs.getInt(HighScores.MAPS_SCORE, 0) + score);
			break;
		case AndroidProject.QUIZ_CAPITALS:
			editor.putInt(HighScores.CAPITALS_TIMES, prefs.getInt(HighScores.CAPITALS_TIMES, 0) + 1);
			if (score == 10) {
				editor.putInt(HighScores.CAPITALS_WINS, prefs.getInt(HighScores.CAPITALS_WINS, 0) + 1);
			}
			editor.putInt(HighScores.CAPITALS_SCORE, prefs.getInt(HighScores.CAPITALS_SCORE, 0) + score);
			break;
		}
		editor.commit();
	}
	
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_GAME_OVER:
			AlertDialog.Builder gameOverDialogBuilder = new AlertDialog.Builder(this);
			gameOverDialogBuilder.setTitle(R.string.lblGameOverTitle);
			
			String message; 
			message = correct == 10 ? 
					getResources().getString(R.string.lblGameOverG):
					getResources().getString(R.string.lblGameOverB);
			message += correct;
			gameOverDialogBuilder.setMessage(message);
			
			gameOverDialogBuilder.setCancelable(false);
			gameOverDialogBuilder.setPositiveButton(R.string.lblClose, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					QuizActivity.this.finish();
				}
			});
			dialog = gameOverDialogBuilder.create();
			break;
		}
		return dialog;
	}
	
}
